/**
 * Created by Administrator on 2016/11/8.
 */
class ResultPlayerItem extends BaseSprite {

    spacing = 105;

    private lab_nick: eui.Label;
    private lab_uid: eui.Label;
    private lab_description: eui.Label;
    private paiGroup: eui.Group;
    private lab_hu: eui.Label;
    private lab_gang: eui.Label;
    private lab_zong: eui.Label;
    private img_state: eui.Image;
    /**番型文本超过两行显示出 */
    private btn_rule_desc: eui.Group;
    img_line: eui.Image;

    private tips: TextTips;

    headIcon: HeadIcon;
    cardViews: CardView[];
    pos: egret.Point;

    constructor() {
        super();

        this.skinName = "ResultPlayerItemSkin";

        this.cardViews = [];

        this.tips = new TextTips(this.btn_rule_desc);
        this.tips.offsetPos = { x: -10, y: 0 };
    }

    childrenCreated() {
        super.childrenCreated();
        this.pos = new egret.Point();
    }

    update(person: any) {

        this.pos.x = this.pos.y = 0;

        this.data = person;

        let fanStr: string = this.getDescription(this.data.hu_type);


        this.headIcon.clean();
        this.headIcon.update(person);
        this.headIcon.skinState = "inresult";
        this.headIcon.isZhuang = GSData.i.result.zhuang == person.pos;

        this.lab_nick.text = "" + this.data.nick;
        this.lab_uid.text = "ID:" + this.data.uid;
        this.lab_description.text = fanStr;
        this.lab_hu.text = "胡:" + this.data.cur;
        this.lab_gang.text = "杠:" + this.data.gang;
        this.lab_zong.text = "合计:" + (this.data.gang + this.data.cur);

        switch (GameConfig.gameType) {
            case GameType.shenyang:
                this.lab_hu.visible = false;
                this.lab_gang.visible = false;
                break;
            case GameType.baotou:
                this.lab_gang.visible = false;
                this.lab_zong.visible = false;
                break;
        }
        /**番型文本全部显示按钮 */
        this.btn_rule_desc.visible = this.lab_description.numLines > 2;
        this.tips.desc = fanStr;

        this.showDown();
        this.showUp();
        this.showHu();
    }

    //显示胡牌
    showHu() {

        this.img_state.visible = true;

        if (this.data.hus.length > 0) {
            this.img_state.source = "result_text_hu";
        }
        else if (GSData.i.result.hupai.pos == this.data.pos) {
            this.img_state.source = "result_text_dianpao";
        }
        else {
            this.img_state.visible = false;
        }
    }

    //显示手牌
    showUp() {

        let pai;
        let o;

        for (let i: number = 0; i < this.data.left.length; i++) {
            pai = this.data.left[i];
            o = GSConfig.getPosByIndex(1, 4, i);
            this.addCardView(pai, this.pos.x + o.x, this.pos.y);
        }

        //判断手牌长度进行间隔错位
        if (GSConfig.allowLens(this.data.left.length)) {
            let cardView: CardView = <CardView>this.paiGroup.getElementAt(this.paiGroup.numElements - 1);
            if (cardView) {
                cardView.posView(cardView.pos.x + 50, cardView.pos.y);
            }
        }
    }

    //显示门前牌
    showDown() {
        let checks: any[] = [1, 2, 23, 24, 25, 26, 224, 225];
        for (let i: number = 0; i < checks.length; i++) {
            let type: number = checks[i];
            let group: any[] = this.data[type];
            let centerO: any;
            if (group && group.length) {
                switch (type) {
                    case 1://吃
                    case 2://碰
                        for (let j: number = 0; j < group.length; j++) {
                            let pais = group[j];
                            for (let k: number = 0; k < pais.length; k++) {
                                let o = GSConfig.getPosByIndex(1, 4, k);
                                this.addCardView(pais[k], this.pos.x + o.x, this.pos.y);
                            }
                            this.pos.x += this.spacing;
                        }
                        break;
                    case 23://旋风杠
                        for (let j: number = 1; j < group.length; j++) {
                            let pai = group[j];
                            let o = GSConfig.getPosByIndex(1, 4, j - 1);
                            if (j == 2) {
                                centerO = o;
                            }
                            this.addCardView(pai, this.pos.x + o.x, this.pos.y);
                        }
                        this.addCardView(group[0], this.pos.x + centerO.x, this.pos.y - 10);
                        this.pos.x += this.spacing;
                        break;
                    case 24://暗杠
                        for (let j: number = 0; j < group.length; j++) {
                            let pais = group[j];
                            let centerO: any;
                            for (let k: number = 0; k < 3; k++) {
                                let o = GSConfig.getPosByIndex(1, 4, k);
                                if (k == 1) {
                                    centerO = o;
                                }
                                this.addCardView(pais[k], this.pos.x + o.x, this.pos.y, null, 0.55, 2);
                            }

                            this.addCardView(pais[0], this.pos.x + centerO.x, this.pos.y - 10);

                            this.pos.x += this.spacing;
                        }
                        break;
                    case 25://明杠
                        for (let j: number = 0; j < group.length; j++) {
                            let pais = group[j];
                            let centerO: any;
                            for (let k: number = 0; k < 3; k++) {
                                let o = GSConfig.getPosByIndex(1, 4, k);
                                if (k == 1) {
                                    centerO = o;
                                }

                                this.addCardView(pais[k], this.pos.x + o.x, this.pos.y);
                            }

                            this.addCardView(pais[0], this.pos.x + centerO.x, this.pos.y - 10);

                            this.pos.x += this.spacing;
                        }
                        break;
                    case 26://中发白杠
                        for (let j: number = 0; j < group.length; j++) {
                            let pai = group[j];
                            let o = GSConfig.getPosByIndex(1, 4, j);
                            this.addCardView(pai, this.pos.x + o.x, this.pos.y);
                        }
                        this.pos.x += this.spacing;
                        break;
                }
            }
        }
    }

    private addCardView(pai, x, y, count?, scale?, style?) {
        let card = CardView.create(1, style ? style : 4, pai, count ? count : 1);
        card.posView(x, y + 20);
        scale && (card.scaleX = card.scaleY = scale);
        this.paiGroup.addChild(card);

        this.cardViews.push(card);

        return card;
    }

    clear() {
        while (this.cardViews.length) {
            let cardView: CardView = <CardView>this.cardViews.shift();
            this.paiGroup.removeChild(cardView);

            CardView.returnCardView(cardView);
        }
    }

    private getDescription(list?: any[]) {
        if (!list) return;

        function append_number(n) {
            return GameRules.resultText(n);
        }

        function append_list(list) {
            return GameRules.resultText(list[0]) + list[1];
        }


        let desc: string = "";
        let item;
        let extend;
        for (let i: number = 0; i < list.length; i++) {
            item = list[i];

            if (typeof item == "number") {
                desc += append_number(item);
            }
            else if (typeof item == "string") {
                desc += item;
            }
            else if (typeof item[1] == 'string') {
                desc += append_list(item);
            }
            else {
                desc += append_number(item[0]);
                desc += "(";

                extend = item[1];
                for (let j: number = 0; j < extend.length; j++) {
                    if (typeof extend[j] == "number") {
                        desc += append_number(extend[j]);
                    }
                    else if (typeof extend[j] == "string") {
                        desc += extend[j];
                    }
                    else if (typeof extend[j][1] == 'string') {
                        desc += append_list(extend[j]);
                    }

                    //补空位
                    if (j != extend.length - 1) desc += " ";
                }

                desc += ")";
            }

            if (i != list.length - 1) desc += " ";
        }

        return desc;
    }
}